// pages/comment/index.js
var app=getApp();
Page({
  data: {    
    topicObej: {},
    img_arr: [],
    inputValue:'',
    inputTitle:'',
    conversation:[],
    aliyunOss:'',
    itemId:[]
  },
  onLoad: function (options) {
    let list = JSON.parse(options.dataList);
    console.log(list);
    this.setData({
      conversation:list,
      aliyunOss:app.data.aliyunOss
    })
  },
  bindGetUserInfo: function (e) {
    console.log(e);   
  },
  upimg: function () {
    var _this = this;
    if (this.data.img_arr.length < 3) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        success: function (res) {
          console.log(res)
          var tempFilePaths=res.tempFilePaths;         
          wx.uploadFile({
            url: app.data.service_url + '/app/uploadfile',
            filePath: tempFilePaths[0],
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            success:function(res){
              var data = JSON.parse(res.data);
              var code = data.code;
              var arry = data.data;
              var aliyunOss=app.data.aliyunOss;
              if (code == "200") {
                _this.setData({
                  img_arr: _this.data.img_arr.concat(aliyunOss+arry[0])
                })
              }else{
                var message = data.message;
                wx.showToast({
                  title: message,
                  icon: 'none',
                  duration: 3000,
                  mask: false
                })
              }
            },
            fail:function(res){
              wx.showToast({
                title: '请求失败',
                icon: 'none',
                duration: 3000,
                mask: false
              })
            }
          })
        },
      })
    } else {
      wx.showToast({
        title: '最多上传三张图片',
        icon: 'loading',
        duration: 3000
      })
    }
  },
  checkboxChange:function(e){
    var arry=e.detail.value;
    this.setData({
      itemId:arry
    })
  },
  inputTitle:function(e){
     this.setData({
       inputTitle: e.detail.value
     })
  },
  inputEvent:function(e){
    this.setData({
      inputValue: e.detail.value
    })
  },
  removeImg:function(e){
    this.data.img_arr.splice(e.currentTarget.dataset.index,1);
    var index = e.currentTarget.dataset.index;
    var arry = this.data.img_arr;
    var _this=this;
    app.paiRequest('/app/moveFile', { filePath: arry[index] }).then(function (res) {
      _this.setData({
        img_arr: _this.data.img_arr
      })
    });
    
  },
  formSubmit:function(e){
    var dataObj=e.currentTarget.dataset;
    console.log(dataObj);
    var data=new Object();
    data.topicTitle = dataObj.topictitle;
    data.topicContent = dataObj.topiccontent;
    data.itemize = dataObj.itemize;
    data.topicImg = this.data.img_arr;
    data.topicImgPath=this.data.img_arr[0];
    data.nickname = app.globalData.userInfo.userName;
    app.paiRequest('/app/topic/conversation/add', data).then(function (res) {
      wx.switchTab({
        url: '/pages/community/index',
      })
    });
  }
})